package cn.micro.keep.accounts.controller;


import cn.micro.keep.accounts.api.Result;
import cn.micro.keep.accounts.common.utils.SecurityUtil;
import cn.micro.keep.accounts.entity.AccountDimCategory;
import cn.micro.keep.accounts.entity.dto.AccountDimCategoryDTO;
import cn.micro.keep.accounts.entity.dto.AccountDimCategorySearch;
import cn.micro.keep.accounts.service.AccountDimCategoryService;
import cn.micro.keep.accounts.util.ValidateGroup;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 账单分类信息表 前端控制器
 * </p>
 *
 * @author 柘阳科技
 * @since 2023-11-08
 */
@RestController
@RequestMapping("/accounts/accountDimCategory")
@RequiredArgsConstructor
@Validated
@Slf4j
public class AccountDimCategoryController {

    private final AccountDimCategoryService accountDimCategoryService;

    @GetMapping("list")
    public Object list(AccountDimCategorySearch search){
        search.setUserId(SecurityUtil.getUserId());
        return new Result<>().success(new PageInfo<>(accountDimCategoryService.listByPage(search)));
    }


    @GetMapping("info/{id}")
    public Object info(@PathVariable Integer id){
        return new Result<>().success(accountDimCategoryService.getById(id));
    }

    @PostMapping("create")
    public Object create(@Validated(value = ValidateGroup.add.class) @RequestBody AccountDimCategoryDTO dto){
        if (!accountDimCategoryService.create(dto)){
            return new Result<>().fail("创建失败");
        }
        return new Result<>().success("创建成功");
    }

    @PutMapping("edit")
    public Object edit(@Validated(value = ValidateGroup.add.class) @RequestBody AccountDimCategoryDTO dto){
        if (!accountDimCategoryService.edit(dto)){
            return new Result<>().fail("修改失败");
        }
        return new Result<>().success("修改成功");
    }

    @DeleteMapping("deleteBatch")
    public Object deleteBatch(List<Integer> ids){
        if (!accountDimCategoryService.removeBatchByIds(ids)){
            return new Result<>().fail("删除失败");
        }
        return new Result<>().success("删除成功");
    }

    /**
     * 删除分类信息
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public Object delete(@PathVariable Integer id){
        LambdaQueryWrapper<AccountDimCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccountDimCategory::getId,id)
                .eq(AccountDimCategory::getUserId,SecurityUtil.getUserId());
        if (!accountDimCategoryService.remove(queryWrapper)){
            return new Result<>().fail("删除失败");
        }
        return new Result<>().success("删除成功");
    }

}
